﻿/*! Copyright (c) 2013 Gins Zhang
 * Version: 1.0.0
 */
(function ($, undefined) {
    $.fn.placeholder = function (options) {
        var defaults = {
            labelStyle: {
                cursor: 'text',
                position: 'absolute',
                zIndex: 5
            }
        };
        var params = $.extend({}, defaults, options || {});

        var nativeSupport = !!('placeholder' in document.createElement('input'));
        if (!nativeSupport) {
            $(this).each(function () {
                var $this = $(this);
                var placeholder = $this.attr('placeholder');
                if (placeholder) {
                    var forId = $this.attr('id');
                    if (!forId) {
                        forId = 'placeholder' + Math.random();
                    }
                    var $placeholder = $('<label for=' + forId + ' class=placeholder />');
                    var css = $.extend({
                        height: $this.css('height'),
                        lineHeight: $this.css('lineHeight'),
                        margin: $this.css('margin'),
                        paddingLeft: $this.css('paddingLeft'),
                        paddingRight: $this.css('paddingRight'),
                        paddingTop: $this.css('paddingTop'),
                        paddingBottom: $this.css('paddingBottom'),
                        display: $this.val() == '' ? 'block' : 'none'
                    }, params.labelStyle);
                    $placeholder.css(css);
                    $($placeholder).on('focus', function () {
                        $this.focus();
                    }).on('click', function () {
                        $this.focus();
                    });
                    $placeholder.text(placeholder);
                    $this.before($placeholder);

                    $this.on('focusin', function () {
                        $placeholder.hide();
                    }).on('focusout', function () {
                        if ($this.val() == '') {
                            $placeholder.show();
                        }
                        else {
                            $placeholder.hide();
                        }
                    });
                }
            });
            return $(this);
        }
        return $(this);
    };
})(jQuery);
$(function () {
    $('input[placeholder],textarea[placeholder]').placeholder()
});